package org.jeecg.eqp.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.eqp.entity.MesStdtimeCfg;
import org.jeecg.eqp.service.IMesStdtimeCfgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 标准工时
 * @Author: licy
 * @Date: 2022-10-12
 * @Version: V1.0
 */
@Api(tags = "标准工时")
@RestController
@RequestMapping("/eqp/mesStdtimeCfg")
@Slf4j
public class MesStdtimeCfgController extends JeecgController<MesStdtimeCfg, IMesStdtimeCfgService> {
    @Autowired
    private IMesStdtimeCfgService mesStdtimeCfgService;
    @Lazy
    @Resource
    private CommonAPI commonApi;

    /**
     * 单表分页列表查询
     *
     * @param mesStdtimeCfg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "标准工时-分页列表查询")
    @ApiOperation(value = "标准工时-分页列表查询", notes = "标准工时-分页列表查询")
    @GetMapping(value = "/list")
    @DS("#header.his")
    public Result<IPage<MesStdtimeCfg>> queryPageList(MesStdtimeCfg mesStdtimeCfg,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<MesStdtimeCfg> queryWrapper = QueryGenerator.initQueryWrapper(mesStdtimeCfg, req.getParameterMap());
        Page<MesStdtimeCfg> page = new Page<MesStdtimeCfg>(pageNo, pageSize);
        IPage<MesStdtimeCfg> pageList = mesStdtimeCfgService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param mesStdtimeCfg
     * @return
     */
    @AutoLog(value = "标准工时-添加")
    @ApiOperation(value = "标准工时-添加", notes = "标准工时-添加")
    @PostMapping(value = "/add")
    @DS("#header.line")
    public Result<String> add(@RequestBody MesStdtimeCfg mesStdtimeCfg) {
        Integer counterValue = commonApi.getCounterValue("recid.mes_stdtime_cfg","0");
        mesStdtimeCfg.setRecId(counterValue);
        mesStdtimeCfgService.save(mesStdtimeCfg);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesStdtimeCfg
     * @return
     */
    @AutoLog(value = "标准工时-编辑")
    @ApiOperation(value = "标准工时-编辑", notes = "标准工时-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("#header.line")
    public Result<String> edit(@RequestBody MesStdtimeCfg mesStdtimeCfg) {
        //框架修改 mesStdtimeCfgService.updateById(mesStdtimeCfg);
        //return Result.OK("编辑成功!");
        return mesStdtimeCfgService.edit(mesStdtimeCfg);
    }

    /**
     * 通过map删除
     *
     * @param map
     * @return
     */
    @AutoLog(value = "标准工时-通过id删除")
    @ApiOperation(value = "标准工时-通过id删除", notes = "标准工时-通过id删除")
    @DeleteMapping(value = "/delete")
    @DS("#header.line")
    public Result<String> delete(@RequestParam Map<String, Object> map) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("REC_ID", map.get("recId"));
        //自定义查询条件，下面的值全部满足才能删除成功
        mesStdtimeCfgService.removeByMap(hashMap);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "标准工时-批量删除")
    @ApiOperation(value = "标准工时-批量删除", notes = "标准工时-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    @DS("#header.line")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.mesStdtimeCfgService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "标准工时-通过id查询")
    @ApiOperation(value = "标准工时-通过id查询", notes = "标准工时-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("#header.line")
    public Result<MesStdtimeCfg> queryById(@RequestParam(name = "id", required = true) String id) {
        MesStdtimeCfg mesStdtimeCfg = mesStdtimeCfgService.getById(id);
        if (mesStdtimeCfg == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mesStdtimeCfg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mesStdtimeCfg
     */
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, MesStdtimeCfg mesStdtimeCfg) {
        return super.exportXls(request, mesStdtimeCfg, MesStdtimeCfg.class, "标准工时");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @DS("#header.line")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile multipartFile = entity.getValue();
            String uploadDir = "D:\\develop\\ccode";
            File uploadDirectory = new File(uploadDir);
            if (!uploadDirectory.exists()) {
                uploadDirectory.mkdir(); // 或者使用 mkdirs 创建多级目录
            }

            // 完整的保存路径
            File destinationFile = new File(uploadDir + File.separator + "123456.png");

            // 保存文件
            multipartFile.transferTo(destinationFile);

        }
        return Result.ok("文件导入成功！");
    }

}
